package cn.edu.nwsuaf.examcenter.service.serviceImpl;


import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import cn.edu.nwsuaf.examcenter.model.PageModel;
import cn.edu.nwsuaf.examcenter.pojo.ExamPaperInfo;
import cn.edu.nwsuaf.examcenter.service.ExamPaperService;

@Service(value="examPaperService")
public class ExamPaperServiceImpl extends BaseServiceImpl<ExamPaperInfo> implements ExamPaperService {

	@Override
	public void loadExamPaperCheck(ModelMap modelMap) {
		// TODO Auto-generated method stub
		PageModel<ExamPaperInfo> pageModel = new PageModel<>();
		String hql = "FROM ExamPaperInfo as paper where paper.examPaperIscheck=0";
		List<ExamPaperInfo> list = super.findAll(hql, null);
		pageModel.setTotalrows(list.size());
		pageModel.setList(list);
		modelMap.addAttribute("pageModel",pageModel);
	}

	@Override
	public ExamPaperInfo getExamPaperInfo(String examPaperNo) {
		// TODO Auto-generated method stub
		return super.loadById(ExamPaperInfo.class, examPaperNo);
	}
	
	public String passExamPaperCheck(String examPaperNo){
		ExamPaperInfo examPaper = super.getById(ExamPaperInfo.class, examPaperNo);
		examPaper.setExamPaperIscheck(1);
		super.updateData(examPaper);
		
		return examPaper.getCource().getCourceNo();
	}

	@Override
	public void failExamPaperCheck(String examPaperNo) {
		// TODO Auto-generated method stub
		ExamPaperInfo examPaper = super.getById(ExamPaperInfo.class, examPaperNo);
		examPaper.setExamPaperIscheck(2);
		super.updateData(examPaper);
	}

	@Override
	public void loadExamPaper(ModelMap modelMap) {
		// TODO Auto-generated method stub
		PageModel<ExamPaperInfo> pageModel = new PageModel<>();
		
		String hql = "FROM ExamPaperInfo as paper where paper.examPaperIscheck=1";
		String[] param = null;
		
		List<ExamPaperInfo> list = super.findAll(hql, param);
		pageModel.setTotalrows(list.size());
		pageModel.setList(list);
		modelMap.addAttribute("pageModel",pageModel);
	}
	@Override
	public void loadExamList(List<String> examList, List<String> courceList) {
		// TODO Auto-generated method stub
		for(int i = 0; i<courceList.size(); i++){
			String hql = "FROM ExamPaperInfo where cource.courceNo=?";
			String[] param = {courceList.get(i)};
			List<ExamPaperInfo> list = super.findAll(hql, param);
			
			if(list.size()==0){
				examList.add(null);
			}else{
				examList.add(list.get(0).getExamPaperNo());
			}
		}
	}
}
